//
//  SearchFriendResultVC.m
//  ChangChat
//
//  Created by Cocoa on 2019/9/5.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "SearchFriendResultVC.h"
#import "FriendCell.h"
#import "UIImage+Gradient.h"

@interface SearchFriendResultVC ()
@property (nonatomic, strong) NSArray *resultArray;
@end

@implementation SearchFriendResultVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
//    self.extendedLayoutIncludesOpaqueBars = !self.navigationController.navigationBar.translucent;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
}

- (void)setupUI {
    [self.tableView customRegisterClassWithName:@"FriendCell"];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
}

#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSLog(@"update search result = %@", searchController.searchBar.text);
    NSString *searchText = searchController.searchBar.text;
    
    if (searchText.isHaveStr) {
        
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username CONTAINS %@ || nickname CONTAINS %@ || pinyin CONTAINS[c] %@ || friend_id = %@", searchText, searchText, searchText, searchText];
        NSArray *itemArr = [self.dataSource filteredArrayUsingPredicate:predicate];
        self.resultArray = itemArr;
        
    } else {
        self.resultArray = nil;
    }
    [self.tableView reloadData];
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
}

#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"%s", __func__);
}

#pragma mark - UISearchControllerDelegate
- (void)willPresentSearchController:(UISearchController *)searchController {
//    [searchController.searchBar setBackgroundImage:UIImage.new];
    self.view.alpha = 1.0;
//    [searchController.searchBar setPositionAdjustment:UIOffsetMake(0, 0) forSearchBarIcon:UISearchBarIconSearch];
    if (self.viewWillPresentBlock) {
        self.viewWillPresentBlock();
    }
}
- (void)didPresentSearchController:(UISearchController *)searchController {
    for (UIButton *obj in searchController.searchBar.subviews.firstObject.subviews) {
        if ([obj isKindOfClass:UIButton.class] == YES) {
            obj.k_titleColor(UIColor.k_t2Color);
            obj.k_titleColor_state(UIColor.k_t3Color, UIControlStateHighlighted);
        }
    }
    self.dataSource = kUsermanager.friendArray;
    
    [self.navigationController.view setBackgroundColor:UIColor.whiteColor];
}

- (void)willDismissSearchController:(UISearchController *)searchController {
    UIImage *btnBackgroundImg = [[UIImage new] createImageWithSize:CGSizeMake(ScreenWidth, NavigationBarHeight) gradientColors:@[UIColor.k_hexColor(0x6BA4FE), UIColor.k_hexColor(0x3A85F9)] percentage:@[@0.0,@1.0] gradientType:GradientFromLeftToRight];
    [[UINavigationBar appearance] setBackgroundImage:btnBackgroundImg forBarMetrics:UIBarMetricsDefault];
    self.navigationController.view.backgroundColor = [UIColor colorWithPatternImage:btnBackgroundImg];
    if (self.viewWillDisPresentBlock) {
        self.viewWillDisPresentBlock();
    }
//    CGFloat offsetX = (self.view.bounds.size.width - 200 - 32) / 2;
//    [searchController.searchBar setPositionAdjustment:UIOffsetMake(offsetX, 0) forSearchBarIcon:UISearchBarIconSearch];
    
//    [UIView animateWithDuration:0.25 animations:^{
//        self.view.alpha = 0;
//    }];
}
- (void)didDismissSearchController:(UISearchController *)searchController {
    
//    NSLog(@"-------------view= %@", self.tbvc.view);
//    NSLog(@"-------------table= %@", self.tbvc.tableView);
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.resultArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FriendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FriendCell" forIndexPath:indexPath];
    
    FriendItem *friend = self.resultArray[indexPath.row];
    cell.friendItem = friend;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.didSelectFriendItemBlock) {
        self.didSelectFriendItemBlock(self.resultArray[indexPath.row]);
    }
}

@end
